<template>
	<view class="searchLayout">
		<view class="search">
			<uni-search-bar 
			@confirm="onSearch"
			@cancel="onClear"
			@clear="onClear"
			focus 
			placeholder="搜索"
			v-model="queryParams.keyword">
			</uni-search-bar>
		</view>
		
		
		<view v-show="!noSearch && !classList.length">
			<view class="history" v-if="historySearch.length">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="removeHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{tab}}</view>		
				</view>
			</view>
			
			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>				
				</view>
				<view class="tabs">				
					<view class="tab" v-for="tab in recommendList" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>
		</view>
		
		
		<view class="noSearch" v-show="noSearch">
			<uv-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png"></uv-empty>
		</view>
		
		
		<view>
			<view class="list">
				<view class="item" 
				v-for="(item,index) in classList" :key="item._id" @click="handlePreview(index)">				
					<image :src="item.smallPicurl" mode="aspectFill"></image>				
				</view>
			</view>		
			<view v-if="noData || classList.length"><uni-load-more :status="noData?'noMore':'loading'"/></view>
			<view class="safe-area-inset-bottom"></view>
		</view>
		
		
	</view>
</template>

<script setup>
import {ref} from "vue";
import {onLoad,onUnload,onReachBottom} from "@dcloudio/uni-app";
import { apiGet } from '@/api/apis.js';
import {usePreviewDataStore} from '@/store/preview_data.js'

//查询参数
const queryParams = ref({	
	pageNum:1,
	pageSize:12,
	keyword:""
})

//搜索历史词
const historySearch = ref(uni.getStorageSync('historySearch') || []);

//热门搜索词
const recommendList = ref(["美女","帅哥","宠物","卡通"]);

//没有更多
const noData = ref(false);
//没有搜索结果
const noSearch = ref(false);

//搜索结果列表
const classList = ref([]);

const store = usePreviewDataStore()


//点击搜索
const onSearch = ()=>{
	uni.showLoading()
	
	// 对搜索关键词去首位空格
	const value = queryParams.value.keyword.trim()
	
	if(!value) {
		uni.showToast({
			title:'请输入有效文字',
			icon:"error"
		})
		queryParams.value.keyword=''
		return
	}
	

	historySearch.value=[...new Set([value,...historySearch.value])].slice(0,10)
	
	// 将搜索数据添加到浏览器缓存中
	uni.setStorageSync('historySearch',historySearch.value)
	

	
	// 清空搜索出的图片
	classList.value = []
	
	// 重置网络获取图片的第几页
	queryParams.value.pageNum=1
	
	noSearch.value = false
	
	// 获取数据
	searchData()
}

//点击清除按钮
const onClear = ()=>{
	queryParams.value.keyword = ''
	classList.value = []
	queryParams.value.pageNum=1
	noData.value = false
	noSearch.value = false
	store.deleteList()
}



//点击标签进行搜索
const clickTab = (value)=>{
	onClear()
	queryParams.value.keyword = value
	onSearch()
}


//点击清空搜索记录
const removeHistory = ()=>{
	uni.showModal({
		title:"是否清空历史搜索？",
		success:res=>{
			if(res.confirm){
					uni.removeStorageSync('historySearch')
					historySearch.value=[]
					onClear()
			}
		}
	})
}

//触底加载更多
onReachBottom(()=>{
	
	// 无数据直接退出
	if(noData.value) return
	searchData()
})

//关闭有页面
onUnload(()=>{
	
})

// 查看图片
const handlePreview = (index) => {
	
	uni.navigateTo({
		url: `/pages/preview/preview?index=${index+1}`
	});
}

// 获取数据
const searchData = async () => {
	try{
		const res = (await apiGet('searchWall',{keyword:queryParams.value.keyword,pageNum:queryParams.value.pageNum,pageSize:queryParams.value.pageSize})).data
		queryParams.value.pageNum++
		classList.value.push(...res)
		
		if(res.length%12!=0) noData.value = true
		if(classList.value.length==0) noSearch.value = true
		store.updateList(res)
		console.log(classList.value.length,noSearch.value);
	}finally{
		uni.hideLoading()
	}
}


</script>

<style lang="scss" scoped>
.searchLayout{
	.search{
		padding:0 10rpx;
	}
	.topTitle{
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 32rpx;
		color:#999;
	}
	.history{
		padding:30rpx;		
	}
	.recommend{
		padding:30rpx;
	}
	.tabs{
		display: flex;		
		align-items: center;
		flex-wrap: wrap;
		padding-top:20rpx;
		.tab{
			background: #F4F4F4;
			font-size: 28rpx;
			color:#333;
			padding:10rpx 28rpx;
			border-radius: 50rpx;
			margin-right: 20rpx;
			margin-top: 20rpx;
		}
	}	
	.list{
		display: grid;
		grid-template-columns: repeat(3,1fr);
		gap: 5rpx;
		padding:20rpx 5rpx;		
		.item{
			height: 440rpx;
			image{
				width: 100%;
				height: 100%;
				display: block;
			}			
		}		
	}
}
</style>